import matplotlib.pyplot as plt
import numpy as np
from pylab import mpl

import pandas as pd


def plt2(lists):
    print(lists)
    shops = [i[3] for i in lists]
    prices = [i[4] for i in lists]
    items = {}
    for s, p in zip(shops, prices):
        if s in items:
            items[s].append(p)
        else:
            items[s] = [p]
    for k, v in items.items():
        items[k] = np.mean(v)
    print(items)
    plt.pie(items.values(),
            labels=items.keys(),  # 设置饼图标签
            # colors=["#d5695d", "#5d8ca8", "#65a479", "#a564c9", "#d5695d", "#5d8ca8", "#65a479", "#a564c9", "#d5695d",
            #         "#5d8ca8", "#65a479"],  # 设置饼图颜色
            autopct='%0.2f%%',
            )
    plt.title("vivo手机均价", fontproperties='SimHei', fontsize=35)  # 设置标题
    plt.savefig('饼图.jpg', dpi=1000)
    plt.show()


def plt1(lists):
    # 评论数
    comments = [i[5] for i in lists]
    # 手机型号
    model = [i[2] for i in lists]
    print(comments)
    print(model)

    plt.figure(figsize=(20, 15))
    plt.bar(model, comments,color="red")
    plt.xlabel("手机型号",fontsize=35)
    plt.ylabel("评论数",fontsize=35)
    x_t = list(range(len(model)))
    plt.xticks(x_t, model, rotation=20)
    plt.title('vivo手机评论热度分析', fontproperties='SimHei', fontsize=35)
    for a, b in zip(model, comments):
        plt.text(a, b, b)
    plt.savefig('柱状图.jpg', dpi=1000)
    plt.show()


if __name__ == "__main__":
    df = pd.read_csv('京东数据.csv')
    df = df.values.tolist()

    for item in df:
        if '万+' in item[5]:
            item[5] = item[5][:-2]
            item[5] = item[5] + '0000'
        if '+' in item[5]:
            item[5] = item[5][:-1]
    df.sort(key=lambda x: int(x[5]), reverse=0)
    # print(df)
    list1 = df[-10:]
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    plt1(list1)
    plt2(df)
